import createjs from 'createjs-ts';
import { getSnakeBodyImg } from '@/assets/snake/index'
class Crumb extends createjs.Container {

  constructor(color, radius, type) {
    super();
    this.type = type || 'CRUMB'
    this.foreGround = color;
    this.foodRadius = radius || 20;

    if(this.type === 'PROP') {
      this.drawPropBody()
      this.propType = 'invincible'
    } else {
      this.drawBody();
      this.text = ''// traditions[parseInt(traditions.length * Math.random())];
      // this.drawText();
    }
  }

  drawBody() {
    let baseCircle = new createjs.Shape();
    let color = this.foreGround;
    if(this.type === 'DIE_SNAKE') {
      baseCircle.graphics.beginFill(color).drawCircle(0, 0, this.foodRadius).beginFill('#fff').drawCircle(0, 0, this.foodRadius * 0.2).endFill();
    } else {
      baseCircle.graphics.beginFill(color).drawCircle(0, 0, this.foodRadius).endFill();
    }
    
    baseCircle.x = 0;
    baseCircle.y = 0;

    // hui画图片中的内容
    // const foodBody = getSnakeBodyImg()
    // const snakeBodyBitMap = new createjs.Bitmap(foodBody)
    // snakeBodyBitMap.scaleX = 20 / foodBody.width
    // snakeBodyBitMap.scaleY = 20 / foodBody.height
    //     snakeBodyBitMap.x = -10
    //     snakeBodyBitMap.y = -10
    // const snakeBodyMask = new createjs.Shape(); 
    // snakeBodyMask.graphics.beginFill(color).drawCircle(0, 0, 12).endFill();

    this.addChild(baseCircle);
    // this.addChild(snakeBodyMask)
    // this.addChild(snakeBodyBitMap)
    this.setBounds(-6, -6, 12, 12);
  }

  drawText() {
    let text = new createjs.Text(this.text, "24px Arial", "#ff7700");
    text.x = -text.getMeasuredWidth() / 2;
    text.y = -28;
    text.textBaseline = "alphabetic";
    this.addChild(text);
  }

  /**
   * 绘画道具
   * @param {*} color 
   * 
   */
  drawPropBody() {
    let baseCircle = new createjs.Shape();
    let color = this.foreGround;
    // beginFill('#32cef8').drawCircle(0, 0, 40).beginFill('#fff').drawCircle(0, 0, 20)
    baseCircle.graphics.beginFill('#32cef8').drawCircle(0, 0, 80).beginFill('#fff').drawCircle(0, 0, 40).endFill();
    baseCircle.x = 0;
    baseCircle.y = 0;
    baseCircle.alpha = 1;
    this.addChild(baseCircle);
    this.setBounds(-6, -6, 12, 12);
    this.timer = setInterval(() => {
      if(this.propInter === '-') {
        baseCircle.alpha -= 0.1;
        baseCircle.scale -= 0.02
        if(baseCircle.alpha <= 0.3) {
          this.propInter = '+';
        }
      } else {
        baseCircle.alpha += 0.1;
        baseCircle.scale += 0.02
        if(baseCircle.alpha >= 1) {
          this.propInter = '-';
        }
      }
    }, 100);
  }
  clearPropInterVal() {
    clearInterval(this.timer);
  }
  setForeGround(color) {
    this.foreGround = color;
    this.removeAllChildren();
    this.drawBody();
  }
}
export default Crumb